package cn.gson.crm.controller.student;
import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.dao.CustomDao;
import cn.gson.crm.model.dao.EmpDao;
import cn.gson.crm.model.dao.OrderDao;
import cn.gson.crm.model.dao.RecordsDao;
import cn.gson.crm.model.entity.Custom;
import cn.gson.crm.model.entity.Employee;
import cn.gson.crm.model.entity.Records;
import cn.gson.crm.vo.OrderVo;
import cn.gson.crm.vo.RecordsVo;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 修改学员信息
 */
@WebServlet("/student/form")
public class StudentFormServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private RecordsDao recordsDao = new RecordsDao();
	private OrderDao orderDao = new OrderDao();
	private CustomDao custDao = new CustomDao();
	private EmpDao empDao = new EmpDao();
	private String stuJsp = "student/stu_form.jsp"; 

	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String sid = request.getParameter("sid");	
		String orderStatusId = request.getParameter("orderStatusId");
		
		//获取订单信息
		OrderVo orderVo = orderDao.getStuById(sid);		
		request.setAttribute("orderVo", orderVo);
		//获取员工信息
		List<Employee> employee = empDao.getEmpList();
		request.setAttribute("employee", employee);
		if(sid != null){		
			RecordsVo records = recordsDao.getRecordsByNameId(Integer.parseInt(sid));
			request.setAttribute("records", records);
		}	
		forward("student/stu_form.jsp");
	}

	protected void post(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String sid = request.getParameter("sid");
		String orderCustName = request.getParameter("orderCustName");
		String recordTotalmoney = request.getParameter("recordTotalmoney");
		String recordPaid = request.getParameter("recordPaid");
		String recordNowpaid = request.getParameter("recordNowpaid");
		String recordUnpaid = request.getParameter("recordUnpaid");
		String recordPayee = request.getParameter("recordPayee");
		String paidall = request.getParameter("paidall");
		String loan = request.getParameter("loan");
		String customStatus = request.getParameter("customStatus");	
		String recordPaidtime = request.getParameter("recordPaidtime");
		RecordsVo records = new RecordsVo();
			records.setOrderCustNameId(Integer.parseInt(sid));
			records.setRecordTotalmoney(recordTotalmoney);
			records.setRecordPayee(recordPayee);
			records.setRecordPayeeId(6);	
			records.setRecordNowpaid(recordNowpaid);
			records.setRecordPaidtime(java.sql.Date.valueOf(recordPaidtime));
			records.setRecordPaid(recordPaid);
			records.setRecordUnpaid(recordUnpaid);
			
			//当学员进行第一次缴费的时候，就可以将其状态变更成付款中
			if(Double.parseDouble(recordPaid) > 0){				
				custDao.updateCustomStatus(3,sid);
			}
			//当学员费用交完时，状态改为已付款
			if(Double.parseDouble(recordUnpaid) == 0.0){
				custDao.updateCustomStatus(11,sid);
			}
			
			if("是".equals(paidall)){
				records.setRecordPaidall(1);
			}else{
				records.setRecordPaidall(0);
			}
			if("是".equals(loan)){
				records.setRecordLoan(1);
			}else{
				records.setRecordLoan(0);
			}
			
		
		request.setAttribute("records", records);				
		if(recordNowpaid == null || recordNowpaid.isEmpty() ){			
			errorForward("本次支付金额不能为空", stuJsp);
			return;
		}
		
		if(recordPayee == null || recordPayee.isEmpty()){
			errorForward("收款人不能为空", stuJsp);
		}
		
		
		boolean result = recordsDao.insertRecords(records);
		if(result){	
			redirect("student");
		}else{
			errorForward("提交失败", stuJsp);
		}
		
	}

}
